from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from .models import Material
from django.http import JsonResponse

@login_required
def material_library(request):
    materials = Material.objects.filter(user=request.user).order_by('-upload_time')
    return render(request, 'material_library.html', {'materials': materials})

@login_required
def delete_material(request, material_id):
    material = get_object_or_404(Material, id=material_id, user=request.user)
    material.delete()
    return JsonResponse({'status': 'success'})

from django.shortcuts import render, redirect
from .forms import MaterialUploadForm
from django.contrib.auth.decorators import login_required

@login_required
def upload_material(request):
    if request.method == 'POST':
        form = MaterialUploadForm(request.POST, request.FILES)
        if form.is_valid():
            material = form.save(commit=False)
            material.user = request.user
            material.save()
            return redirect('sourcematerial:material_library')
    else:
        form = MaterialUploadForm()
    return render(request, 'upload_material.html', {'form': form})